package lol.clann.minecraft.springboot.bukkit.utils;

import lol.clann.minecraft.springboot.bukkit.plugin.Adapter;
import lol.clann.minecraft.springboot.bukkit.plugin.AdapterContext;
import org.bukkit.Bukkit;

import java.util.Arrays;

/**
 *
 * @author pyz
 * @date 2019/6/24 5:54 PM
 */
public class BukkitUtils {
    private static final String bukkitVersion = Bukkit.getBukkitVersion();
    private static final String packageVersion = AdapterContext.getPackageVersion();
    private static final int bukkitVersionNumber;

    static {
        String version = bukkitVersion;
//        通过系统变量暴露给spring
        System.setProperty("bukkit.version", Bukkit.getBukkitVersion());
        AdapterContext.platformInfo.put("bukkitVersion", Bukkit.getBukkitVersion());
        Adapter.getLogger().info("bukkit verion:" + Bukkit.getBukkitVersion());
        int sep = version.indexOf('-');
        if (sep >= 0) {
            version = version.substring(0, sep);
        }
        String[] parts = version.split("\\.");
        Adapter.getLogger().info("bukkit verion parts:" + Arrays.toString(parts));
        int number = 0;
        number += Integer.valueOf(parts[2]);
        number += Integer.valueOf(parts[1]) * 100;
        number += Integer.valueOf(parts[0]) * 10000;
        bukkitVersionNumber = number;

        System.setProperty("bukkit.version.number", String.valueOf(number));
        AdapterContext.platformInfo.put("bukkitVersionNumber", Bukkit.getBukkitVersion());
        Adapter.getLogger().info("bukkit verion number:" + number);
        Adapter.getLogger().info("packet verion:" + packageVersion);
        Adapter.getLogger().info("remapping resource:" + String.valueOf(AdapterContext.getRemappingResource()));
    }

    public static String getBukkitVersion() {
        return bukkitVersion;
    }

    public static String getPackageVersion() {
        return packageVersion;
    }

    public static int getBukkitVersionNumber() {
        return bukkitVersionNumber;
    }

    public static void printBukkitVersions() {
        Adapter.getLogger().error("bukkit verion:" + Bukkit.getBukkitVersion());
        Adapter.getLogger().error("bukkit verion number:" + bukkitVersionNumber);
        Adapter.getLogger().error("packet verion:" + packageVersion);
        Adapter.getLogger().error("remapping resource:" + String.valueOf(AdapterContext.getRemappingResource()));
    }

}
